package io.hepu.robotize.config;

import io.hepu.robotize.enums.Permissions;
import io.hepu.robotize.model.Permission;
import io.hepu.robotize.service.IPermissionService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.PropertySource;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Data
@Slf4j
@Configuration
@DependsOn("jedisPool")
@ConfigurationProperties(prefix = "shiro")
@PropertySource(value = "classpath:application-shiro.yml", factory = YamlPropertySourceFactory.class)
public class ShiroProperties {
    private final IPermissionService permService;
    private List<String> whitelist = new ArrayList<>();
    private List<Permission> permissions = new ArrayList<>();

    public ShiroProperties(IPermissionService permService) {
        this.permService = permService;
    }

    @PostConstruct
    public void init() {
        permissions = permService.list(Collections.emptyMap()).stream()
                .filter(perm -> perm.getType() == Permissions.ACTION)
                .collect(Collectors.toList());
        log.info("Permissions loaded successfully. Total: {}", permissions.size());
    }
}
